import {getInfo, login, logout} from '@/api/login'
import {getToken, removeToken, setToken, getCurrentUserMenu} from '@/utils/auth'
import {default as api} from '../../utils/api'
import store from '../../store'
import router from '../../router'
import {Message, MessageBox} from 'element-ui'

const user = {
  state: {
    nickname: "",
    userId: "",
    username: "",
    orgCode: "",
    idcard: "",
    policeNum: "",
    zwlb: "",
    zwjb: "",
    zw: "",
    avatar: './assets/user.jpg',
    role: null,
    menus: null,
    permissions: null,
    org: null,
    btns: []
  },
  mutations: {
    SET_USER: (state, userInfo) => {
      state.nickname = userInfo.name;
      state.userId = userInfo.id;
      state.username = userInfo.username;
      state.idcard = userInfo.idcard;
      state.policeNum = userInfo.policeNum;
      state.zwlb = userInfo.zwlb;
      state.zwjb = userInfo.zwjb;
      state.zw = userInfo.zw;
      state.role = userInfo.allResourcesTree;
      state.orgCode = userInfo.currentOrg;
      state.org = userInfo.org;
      // state.menus = userInfo.permissionList;
    },
    RESET_USER: (state) => {
      state.nickname = "";
      state.userId = "";
      state.username = "";
      state.orgCode = "";
      state.idcard = "";
      state.policeNum = "";
      state.zwlb = "";
      state.zwjb = "";
      state.zw = "";
      state.role = null;
      state.menus = null;
      state.org = null;
      state.btns = [];
    }
  },
  actions: {
    // 登录
    Login({commit, state}, loginForm) {
      return new Promise((resolve, reject) => {
        api({
          url: "/sys/login",
          method: "post",
          data: loginForm
        }).then(data => {
          if (data.code === 200) {
            //cookie中保存前端登录状态
            setToken(data.data.access_token);
          }
          resolve(data);
        }).catch(err => {
          reject(err)
        })
      })
    },
    // 获取用户信息
    GetInfo({commit, state}) {
      return new Promise((resolve, reject) => {
        api({
          url: '/bucket-system/system/user/getUser',
          method: 'get',
          headers: {
            'Authorization': getToken()
          },
          data: {}
        }).then(data => {
          //储存用户信息
          commit('SET_USER', data.data);
          // TODO cookie保存登录状态,仅靠vuex保存的话,页面刷新就会丢失登录状态

          //获取待办数量
          // store.dispatch('InitMsgNum',{});

          //生成路由
          let userPermission = data.data;
          store.dispatch('GenerateRoutes', userPermission).then(() => {
            //生成该用户的新路由json操作完毕之后,调用vue-router的动态新增路由方法,将新路由添加
            router.addRoutes(store.getters.addRouters)
          })
          resolve(data)
        }).catch(error => {
          commit('RESET_USER');
          removeToken();
          reject(error)
        });
      })
    },
    // 登出
    LogOut({commit}) {
      return new Promise((resolve) => {
        api({
          url: "/cloud-auth/logout/" + getToken(),
          method: "get",
          headers: {
            'Authorization': getToken()
          },
        }).then(data => {
          Message({
            message: '退出成功',
            type: 'success',
            duration: 3 * 1000
          });
          removeToken();
          commit('RESET_USER')
          resolve(data);
        }).catch(() => {
          commit('RESET_USER')
        })

      })
    },
    // 前端 登出
    FedLogOut({commit}) {
      return new Promise(resolve => {
        commit('RESET_USER');
        removeToken();
        resolve()
      })
    }
  }
}
export default user
